package 枚举类;

/*
    枚举类的使用
        1、理解:类的对象只有有限个,确定的,将此类称之为枚举类
        2、当需要定义一组常量时,强烈建议使用枚举类
        3、如果枚举类中只有一个对象,则可以作为单例模式的实现方式

    枚举类的实现
         JDK1.5之前需要自定义枚举类
         JDK1.5新增的 enum 关键字用于定义枚举类

    枚举类的属性
         枚举类对象的属性不应允许被改动,所以应该使用 private final修饰
         枚举类的使用 private final修饰的属性应该在构造器中为其赋值
         若枚举类显式的定义了带参数的构造器,则在列出枚举值时也必须对应的传入参数
 */
public class EnumTest01 {
    public static void main(String[] args) {
        Season1 spring = Season1.SPRING;
        System.out.println(spring);
    }
}

//自定义枚举类
class Season1{

    //1、对象如果有实例变量,应该声明为private final,并在构造器中初始化
    private final String seasonName;
    private final String seasonDesc;

    //2、私有化类的构造器,保证不能在类的外部创建其对象,并给对象属性赋值
    private Season1(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }

    //3、提供当前枚举类的多个对象,声明为:public static final
    public static final Season1 SPRING = new Season1("春天","春暖花开");
    public static final Season1 SUMMER = new Season1("夏天","夏日炎炎");
    public static final Season1 AUTUMN = new Season1("秋天","秋高气爽");
    public static final Season1 WINTER = new Season1("冬天","冰天雪地");

    //其他诉求:获取枚举类对象的属性
    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

    //其他诉求:提供toString方法
    @Override
    public String toString() {
        return "Season1{" +
                "seasonName='" + seasonName + '\'' +
                ", seasonDesc='" + seasonDesc + '\'' +
                '}';
    }
}

